package com.gouaixin.bee.beeframework2.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gouaixin.bee.beeframework2.tool.mLog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.USER;
import com.gouaixin.bee.beeframework2.extra.im.ChatData;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

import cn.bmob.v3.BmobPushManager;
import cn.bmob.v3.BmobUser;


/**
 * Created by Administrator on 2015/3/16.
 */

//应用外，弹出的聊天对话框
public class MessadialogActivity extends Activity  {
    Context context=MessadialogActivity.this;
    View view;

    String fid;//原来的发送方的id

    BmobPushManager bmobPushManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bmobPushManager = new BmobPushManager(this);

        //取消标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //初始化布局
        initLayout();

        //接受intent传过来的json
        initIntent();


    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Intent it=intent;
        String  json=it.getStringExtra("json");
//        if(json!=null&&!json.equals("")){
//
//        }

        //对消息内容进行解析
        mLog.e("对传过来的json进行自定义解析:");

        ChatData chatData = new Gson().fromJson(json, new TypeToken<ChatData>(){}.getType());
        mLog.e("我用gosn解析json消息内容:" + chatData.getContent());

        fName.setText(chatData.getFname());
        fText.setText(chatData.getContent());

        fid=chatData.getFid();
    }

    /***
     * 接受intent传过来的json
     */


    private void initIntent() {
        Intent it=getIntent();
        String  json=it.getStringExtra("json");

        //对消息内容进行解析
        mLog.e("对传过来的json进行自定义解析:");

        ChatData chatData = new Gson().fromJson(json, new TypeToken<ChatData>(){}.getType());
        mLog.e("我用gosn解析json消息内容:" + chatData.getContent());

        fName.setText(chatData.getFname());
        fText.setText(chatData.getContent());

        fid=chatData.getFid();




//        public static final int SEND = 1;//发送
//        public static final int RECEIVER = 2;//接受
//        private String content;//内容
//        private int flag;//标签
//        private String time;//时间
//        private String fid;//发送方的id
//        private String tid;//接受方的id



    }

    /***
     * 初始化布局，按钮的监听
     */
    TextView fName;
    TextView fText;
    TextView send;
    EditText editText;
    private void initLayout() {
        view = LayoutInflater.from(context).inflate(R.layout.dialog_liaotiakuang, null);
        setContentView(view);


         fName = (TextView) view.findViewById(R.id.fName);//发送方的昵称
         fText = (TextView) view.findViewById(R.id.fText);//发送方的文本

         send= (TextView) view.findViewById(R.id.fText);//发送
         editText= (EditText) view.findViewById(R.id.input);//输入框

        LinearLayout ss= (LinearLayout) findViewById(R.id.root);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mLog.e("点击发送");



                String editContent=editText.getText().toString();
                sendMessage(context,editContent);//发送方的：时间、jid、昵称、文本；接受方的id
            }
        });




    }

    /***
     *  发送消息的具体方法
     */
    private void sendMessage(Context context,String fText) {


        ChatData chatData=new ChatData();
        chatData.setContent(fText);
        chatData.setFname(BmobUser.getCurrentUser(context, USER.class).getName());
        chatData.setTime(new Date().getTime() + "");
        chatData.setFid(BmobUser.getCurrentUser(context).getUsername());
        chatData.setTid(fid);
        String gsonstr =  new Gson().toJson(chatData);

        mLog.e("我用gosn生成的json:" + gsonstr);

        try {
            bmobPushManager.pushMessageAll(new JSONObject(gsonstr));
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

